package controllers

import "LoginTemplate/models"

type DataBookController struct {
	BaseController
}

type DataBook struct {
	ID               int     `json:"id"`
	SerialNumber     string  `json:"serial_number"`
	Abstract         string  `json:"abstract"`
	OppositeAccounts string  `json:"opposite_accounts"`
	Assist           string  `json:"assist"`
	Name             string  `json:"name"`
	AccountNumber    string  `json:"account_number"`
	BankCard         string  `json:"bank_card"`
	Income           float64 `json:"income"`
	Expenditure      float64 `json:"expenditure"`
	Remark           string  `json:"remark"`
	DataTime         string  `json:"data_time"`
}

//
//// 添加分类
//func (u *ClientController) InsertDayBook() {
//	var client DataBook
//	err := u.ShouldBind(&client)
//	if err != nil {
//		println(err.Error())
//		u.JsonMsg(500, "", "请求异常，请稍候再试")
//	}
//	sql := "insert into client ( client_name,client_phone,client_classify,time,client_follow,promoter_id,community_id ) values (?,?,?,?,?,?,? )"
//	tx := models.DB.Debug().Exec(sql, client.ClientName, client.ClientPhone, client.ClientClassify, client.Time, client.ClientFollow, client.PromoterId, client.CommunityId)
//	if tx.Error != nil {
//		u.JsonMsg(500, "", "失败")
//	}
//	u.JsonMsg(200, "", "成功")
//}

//// 修改分类
//func (u *ClientController) UpdateDayBook() {
//	var client Client
//	err := u.ShouldBind(&client)
//	if err != nil {
//		u.JsonMsg(500, "", "请求异常，请稍候再试")
//	}
//	sql := "update client set client_name = ?,client_phone = ?,client_classify = ?,time = ? ,client_follow = ? ,promoter_id = ? ,community_id = ? where id = ?"
//	tx := models.DB.Debug().Exec(sql, client.ClientName, client.ClientPhone, client.ClientClassify, client.Time, client.ClientFollow, client.PromoterId, client.CommunityId, client.ID)
//	if tx.Error != nil {
//		u.JsonMsg(500, "", "失败")
//	}
//	u.JsonMsg(200, "", "成功")
//}

type DayBookData struct {
	Code        int        `json:"code"`
	Msg         string     `json:"msg"`
	Data        []DataBook `json:"data"`
	Sum         int        `json:"sum"`         //总页数
	CurrentPage int        `json:"currentPage"` //当前页
	TotalNumber int        `json:"totalNumber"` //总条数
}

// 查询分类
func (u *DataBookController) SelectDayBook() {
	var pictureList PictureList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var client []DataBook
	sql := "select * from daybook limit ? , ?"
	tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
	sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM daybook"
	models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
	sqlNumBer := "select count(*) from daybook"
	models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
	if tx.Error != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var picturedata DayBookData
	picturedata.Code = 200
	picturedata.Data = client
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	//return picturedata
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}
